/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entities.Login;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author HTH2207
 */
@Stateless
public class LoginStateless implements LoginStatelessLocal {

    @PersistenceContext(unitName = "TravelLover-ejbPU")
    private EntityManager entityManager;

    public LoginStateless() {
    }

    public List<Login> checkUser(String uName, String uPass) {
        Query query = entityManager.createQuery("Select l From Login l");
        return query.getResultList();
    }

    public void addUser(String userID, String userPass, String name) {
        Login l = new Login(userID, userPass, name);
        entityManager.merge(l);
    }

    public void updateUser(String userID, String userPass, String name) {
        Login l = entityManager.find(Login.class, userID);
        l.setUserPass(userPass);
        l.setName(name);
        entityManager.merge(l);
    }

    public List<Login> getAllUsers() {
        Query query = entityManager.createQuery("Select l From Login l");
        return query.getResultList();
    }

    public List<Login> findUserByName(String name) {
        Query query = entityManager.createQuery("Select l From Login l Where l.Name Like ?");
        query.setParameter(2, name);
        return query.getResultList();
    }

    public void removeUser(String userID) {
        Login l = entityManager.find(Login.class, userID);
        entityManager.remove(l);
    }
    
}
